Class BotBroadcastHandle extends BroadcastHandler;

function PostBeginPlay()
{
	NextBroadcastHandler = Level.Game.BroadcastHandler;
	Level.Game.BroadcastHandler = Self;
}
function bool AllowsBroadcast( actor broadcaster, int Len )
{
	if( NextBroadcastHandler!=None )
		return NextBroadcastHandler.AllowsBroadcast(broadcaster,Len);
	return Super.AllowsBroadcast(broadcaster,Len);
}
function Broadcast( Actor Sender, coerce string Msg, optional name Type )
{
	if( Type=='Say' && PlayerController(Sender)!=None && PlayerController(Sender).Pawn!=None )
	{
		if( Msg~="Follow" )
			OrderBotFollow(PlayerController(Sender));
	}
	if( NextBroadcastHandler!=None )
		NextBroadcastHandler.Broadcast(Sender,Msg,Type);
	else Super.Broadcast(Sender,Msg,Type);
}
function BroadcastTeam( Controller Sender, coerce string Msg, optional name Type )
{
	if( Type=='TeamSay' && PlayerController(Sender)!=None && Sender.Pawn!=None )
	{
		if( Msg~="Follow" )
			OrderBotFollow(PlayerController(Sender));
	}
	if( NextBroadcastHandler!=None )
		NextBroadcastHandler.BroadcastTeam(Sender,Msg,Type);
	else Super.BroadcastTeam(Sender,Msg,Type);
}
function AllowBroadcastLocalized( actor Sender, class<LocalMessage> Message, optional int Switch, optional PlayerReplicationInfo RelatedPRI_1, optional PlayerReplicationInfo RelatedPRI_2, optional Object OptionalObject )
{
	if( NextBroadcastHandler!=None )
		NextBroadcastHandler.AllowBroadcastLocalized(Sender,Message,Switch,RelatedPRI_1,RelatedPRI_2,OptionalObject);
	else Super.AllowBroadcastLocalized(Sender,Message,Switch,RelatedPRI_1,RelatedPRI_2,OptionalObject);
}
function UpdateSentText()
{
	if( NextBroadcastHandler!=None )
		NextBroadcastHandler.UpdateSentText();
	else SentText = 0;
}

final function OrderBotFollow( PlayerController PC )
{
	local Controller C;
	local KFInvBots Cur,Best;
	local float Dist,BDist;

	for( C=Level.ControllerList; C!=None; C=C.nextController )
	{
		if( KFInvBots(C)==None || C.Pawn==None )
			continue;
		if( KFInvBots(C).AssistingPlayer==PC )
			Cur = KFInvBots(C);
		else if( KFInvBots(C).AssistingPlayer==None )
		{
			Dist = VSizeSquared(C.Pawn.Location-PC.Pawn.Location);
			if( Best==None || Dist<BDist )
			{
				BDist = Dist;
				Best = KFInvBots(C);
			}
		}
	}
	if( Best!=None )
	{
		Best.StartAssistPlayer(PC);
		if( Cur!=None )
			Cur.AssistingPlayer = None;
	}
}

defaultproperties
{
}
